package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * setmeal表现层
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @Transactional
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R addSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.addSetmeal(setmealDto);
        return R.success("保存套餐成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @Transactional
    public R listSetmeal(Integer page,Integer pageSize,String name){
        Page listSetmealDto = setmealService.listSetmeal(page, pageSize, name);
        return R.success(listSetmealDto);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     * @throws BusinessException
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R deleteSetmeal(@RequestParam List<Long> ids) throws BusinessException {
        setmealService.deleteSetmeal(ids);
        return R.success("套餐删除成功");
    }

    /**
     * 起售/停售套餐
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @Transactional
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R changeStatus(@RequestParam List<Long> ids,@PathVariable Integer status){
        setmealService.changeStatus(ids,status);
        return R.success("操作成功");
    }

    /**
     * 根据套餐id查询套餐数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Transactional
    public R getSetmealById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getSetmealById(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    @Transactional
    @CacheEvict(value = "setmealCache",key = "'setmeal_'+#setmealDto.categoryId")
    public R saveSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.saveSetmeal(setmealDto);
        return R.success("套餐修改成功");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "'setmeal_'+#categoryId")
    public R getSetmeal(Long categoryId,Integer status){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,categoryId)
                .eq(Setmeal::getStatus,status);
        List<Setmeal> setmealList = setmealService.list(queryWrapper);
        return R.success(setmealList);
    }
}
